package com.msk.web.controller.wlhy.goods;

import cn.hutool.core.util.StrUtil;
import com.google.common.collect.Maps;
import com.msk.common.core.controller.BaseController;
import com.msk.common.core.domain.R;
import com.msk.common.core.domain.model.LoginUser;
import com.msk.common.utils.StringUtils;
import com.msk.framework.web.utils.TokenUtils;
import com.msk.sys.domain.Dict;
import com.msk.sys.domain.User;
import com.msk.sys.service.SystemService;
import com.msk.sys.utils.DictMskUtils;
import com.msk.wlhy.domain.HtCompany;
import com.msk.wlhy.domain.HtLogisticsOrderDetail;
import com.msk.wlhy.service.HtCompanyService;
import com.msk.wlhy.service.HtLogisticsOrderDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;


@Api(tags = "托运单")
@RestController
@RequestMapping("/api/logisticsOrder")
public class HtLogisticsOrderController extends BaseController {

    @Autowired
    private HtLogisticsOrderDetailService htLogisticsOrderDetailService ;
    @Autowired
    private HtCompanyService htCompanyService;
    @Autowired
    private SystemService systemService;

    /**
     * @Description 查询货主托运单列表
     * @Author wlxiang
     * @Date 19:26 2023/6/7
     * @return com.msk.common.core.domain.R<?>
     **/
    @ApiOperation("查询货主托运单列表")
    @PostMapping("/getLogisticsOrderDetailList")
    public R<?> getLogisticsOrderDetailList(@RequestBody HtLogisticsOrderDetail htLogisticsOrderDetail) {
        Map<String, Object> goodsMemberLogin = TokenUtils.getGoodsMemberLogin();
        if(goodsMemberLogin != null){
            String companyId = String.valueOf(goodsMemberLogin.get("company1Id"));
            htLogisticsOrderDetail.setConsigner(companyId);
        }
        htLogisticsOrderDetail.getParams().put("orderByStr","hlod.generate_status,hlod.create_date desc");
        startPageJson(htLogisticsOrderDetail.getParams(),"");
        //根据公司ID查询货主托运单列表
        List<Map<String,Object>> distanceList = htLogisticsOrderDetailService.selectConsignerLogisticsOrderDetailList(htLogisticsOrderDetail);
        List<Dict> publishWayDictList = DictMskUtils.getDictListNew("publish_way");
        for (Map<String,Object> map : distanceList){
            String publishWay = DictMskUtils.getDictLabelNew(String.valueOf(map.get("publishWay")), publishWayDictList, "");
            map.put("publishWay",publishWay);
        }
        return R.appOk(getDataTable(distanceList));
    }
    
    /**
     * @author llh
     * @date 2023/6/19
     * @return com.msk.common.core.domain.R<?>
     * @description 查看货主托运单详情      
     */
    @ApiOperation("查看货主托运单详情")
    @PostMapping("/getLogisticsOrderDetailInfo")
    public R<?> getLogisticsOrderDetailInfo(@RequestBody Map<String,Object> params){
        String logisticsId = StringUtils.getString(params.get("logisticsId"));
        if(StringUtils.isBlank(logisticsId)){
            return R.appFail(400101, "请求参数托运单ID#logisticsId为空");
        }
        Map<String,Object> resultMap = htLogisticsOrderDetailService.getLogisticsInfo(logisticsId);
        String[] strs = {"priceWay","priceType","publishWay","goodsType"};
        for (int i = 0; i < 4; i++) {
            resultMap.put(strs[i],DictMskUtils.getDictLabel(String.valueOf(resultMap.get(strs[i])), StrUtil.toUnderlineCase(strs[i]), ""));
        }
        return R.appOk(resultMap);
    }
    
    /**
     * @author llh
     * @date 2023/6/19
     * @return com.msk.common.core.domain.R<?>
     * @description 货主生成托运单      
     */
    @ApiOperation("货主生成托运单")
    @PostMapping("/wayBillconsignment")
    public R<?> wayBillconsignment(@RequestBody HtLogisticsOrderDetail htLogisticsOrderDetail){
        Map<String,Object> result = Maps.newHashMap();
        try {
            htLogisticsOrderDetail.setGoodsSupplierUser(htLogisticsOrderDetail.getGoodsSupplierUser());
            htLogisticsOrderDetail.setGoodsStartTime(htLogisticsOrderDetail.getGoodsStartTime());
            htLogisticsOrderDetail.setGoodsEndTime(htLogisticsOrderDetail.getGoodsEndTime());
            htLogisticsOrderDetail.setGenerateStatus("1");
            htLogisticsOrderDetail.setIsOverrun("0");
            htLogisticsOrderDetail.setGenerateTime(new Date());
            htLogisticsOrderDetail.preUpdate();
            String message = validateModel(htLogisticsOrderDetail);
            if (StringUtils.isNotBlank(message)){
                result.put("flag", false);
                result.put("message", message);
                return R.appOk(result);
            }
            htLogisticsOrderDetailService.saveShippingOrderform(htLogisticsOrderDetail);
            result.put("flag", true);
            result.put("message", "托运成功");
        } catch (Exception e) {
            result.put("flag", false);
            result.put("message", "保存失败："+e.getMessage());
        }
        return R.appOk(result);
    }
}
